//
//  EFMyOrderViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/10.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyOrderViewController.h"
#import "EFCouponSegmentView.h"
#import "EFMyOrderViewItem.h"
#import "EFMyOrderViewCell.h"
#import "EFOrderProductItem.h"
#import "EFOrderDetailViewController.h"
#import "iToast.h"

@interface EFMyOrderViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)EFCouponSegmentView* segmentView;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(strong,nonatomic)NSMutableArray* dataArray1;
@property(strong,nonatomic)UITableView* tableView1;

@property(strong,nonatomic)NSMutableArray* dataArray2;
@property(strong,nonatomic)UITableView* tableView2;

@property(strong,nonatomic)NSMutableArray* dataArray3;
@property(strong,nonatomic)UITableView* tableView3;

@property(strong,nonatomic)NSMutableArray* dataArray4;
@property(strong,nonatomic)UITableView* tableView4;

@property(strong,nonatomic)NSMutableArray* dataArray5;
@property(strong,nonatomic)UITableView* tableView5;

@end

@implementation EFMyOrderViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"我的订单";
        self.dataArray1 = [NSMutableArray array];
        self.dataArray2 = [NSMutableArray array];
        self.dataArray3 = [NSMutableArray array];
        self.dataArray4 = [NSMutableArray array];
        self.dataArray5 = [NSMutableArray array];
    }
    return self;
}

-(void)initView{
    __weak typeof(self) weakSelf = self;
    [self.segmentView setSegmentArray:@[@"未付款",@"待收货",@"已完成",@"退换货",@"线下支付"] SelectIndex:^(NSInteger index) {
        [weakSelf clickIndex:index];
    }];
    [self.view addSubview:self.segmentView];
    [self.scrollView addSubview:self.tableView1];
    [self.scrollView addSubview:self.tableView2];
    [self.scrollView addSubview:self.tableView3];
    [self.scrollView addSubview:self.tableView4];
    [self.scrollView addSubview:self.tableView5];
    
    [self.view addSubview:self.scrollView];
    
    [self requestReceving];
    [self clickIndex:0];
    
}

- (void)clickIndex:(NSInteger)index{
    [UIView animateWithDuration:0.3f animations:^{
        self.scrollView.contentOffset = CGPointMake(index *kScreenWidth, 0);
    } completion:nil];
    switch (index) {
        case 0:{
            if (self.dataArray1.count == 0) {
                [self requestUnPay];
            }else{
                [self hideEmptyView];
            }
            break;
        }
        case 1:{
            if (self.dataArray2.count == 0) {
                [self requestReceving];
            }else{
                [self hideEmptyView];
            }
            break;
        }
        case 2:{
            if (self.dataArray3.count == 0) {
                [self requestDone];
            }else{
                [self hideEmptyView];
            }
            break;
        }
        case 3:{
            if (self.dataArray4.count == 0) {
                [self requestReturn];
            }else{
                [self hideEmptyView];
            }
            break;
        }
        case 4:{
            if (self.dataArray5.count == 0) {
                [self requestUnderline];
            }else{
                [self hideEmptyView];
            }
            break;
        }
        default:
            break;
    }
}

-(void)requestUnPay{
    NSString* status = @"0";
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getOrderListWithStatus:status page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            [weakSelf.dataArray1 removeAllObjects];
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                
                if (array.count == 0) {
                    [weakSelf showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }else{
                    [weakSelf hideEmptyView];
                    for (NSDictionary *dic in array) {
                        EFMyOrderViewItem *item = [EFMyOrderViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray1 addObject:item];
                    }
                }
                
            }
            [weakSelf.tableView1 reloadData];
            
        }else{
            [[iToast makeText:message]show];
        }
    }];
    
}

-(void)requestReceving{
    NSString* status = @"1";
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getOrderListWithStatus:status page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            [weakSelf.dataArray2 removeAllObjects];
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                if (array.count == 0) {
                     [weakSelf showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }else{
                    [weakSelf hideEmptyView];
                    for (NSDictionary *dic in array) {
                        EFMyOrderViewItem *item = [EFMyOrderViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray2 addObject:item];
                    }
                }
            }
            [weakSelf.tableView2 reloadData];
            
        }else{
            [[iToast makeText:message]show];
        }
    }];

}

-(void)requestDone{
    NSString* status = @"2";
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getOrderListWithStatus:status page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            [weakSelf.dataArray3 removeAllObjects];
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                if (array.count == 0) {
                     [weakSelf showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }else{
                    [weakSelf hideEmptyView];
                    for (NSDictionary *dic in array) {
                        EFMyOrderViewItem *item = [EFMyOrderViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray3 addObject:item];
                    }
                }
            }
            [weakSelf.tableView3 reloadData];
            
        }else{
            [[iToast makeText:message]show];
        }
    }];

}

-(void)requestReturn{
    NSString* status = @"3";
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getOrderListWithStatus:status page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            [weakSelf.dataArray4 removeAllObjects];
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                if (array.count == 0) {
                    [weakSelf showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }else{
                    [weakSelf hideEmptyView];
                    for (NSDictionary *dic in array) {
                        EFMyOrderViewItem *item = [EFMyOrderViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray4 addObject:item];
                    }
                }
                
            }
            [weakSelf.tableView4 reloadData];
            
        }else{
            [[iToast makeText:message]show];
        }
    }];
}

-(void)requestUnderline{
    NSString* status = @"4";
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getOrderListWithStatus:status page:page size:size OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.HUD hide:YES];
        if(code == 0){
            [weakSelf.dataArray5 removeAllObjects];
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                if (array.count == 0) {
                    [weakSelf showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }else{
                    [weakSelf hideEmptyView];
                    for (NSDictionary *dic in array) {
                        EFMyOrderViewItem *item = [EFMyOrderViewItem yy_modelWithDictionary:dic];
                        [weakSelf.dataArray5 addObject:item];
                    }
                }
               
            }
            [weakSelf.tableView5 reloadData];
            
        }else{
            
        }
    }];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView1) {
        return self.dataArray1.count;
    }else if (tableView == self.tableView2) {
        return self.dataArray2.count;
    }else if (tableView == self.tableView3) {
        return self.dataArray3.count;
    }else if (tableView == self.tableView4) {
        return self.dataArray4.count;
    }else if (tableView == self.tableView5) {
        return self.dataArray5.count;
    }
    
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }else if (tableView == self.tableView2) {
        item =  self.dataArray2[indexPath.row];
    }else if (tableView == self.tableView3) {
        item =  self.dataArray3[indexPath.row];
    }else if (tableView == self.tableView4) {
        item =  self.dataArray4[indexPath.row];
    }else if (tableView == self.tableView5) {
        item =  self.dataArray5[indexPath.row];
    }
    
    EFMyOrderViewItem* orderItem = ( EFMyOrderViewItem* )item;
    EFOrderDetailViewController *vc = [[EFOrderDetailViewController alloc] initWithOrderNumber:orderItem.order_no];
    [ICNavigator pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }else if (tableView == self.tableView2) {
        item =  self.dataArray2[indexPath.row];
    }else if (tableView == self.tableView3) {
        item =  self.dataArray3[indexPath.row];
    }else if (tableView == self.tableView4) {
        item =  self.dataArray4[indexPath.row];
    }else if (tableView == self.tableView5) {
        item =  self.dataArray5[indexPath.row];
    }
    
    return item.viewHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }else if (tableView == self.tableView2) {
        item =  self.dataArray2[indexPath.row];
    }else if (tableView == self.tableView3) {
        item =  self.dataArray3[indexPath.row];
    }else if (tableView == self.tableView4) {
        item =  self.dataArray4[indexPath.row];
    }else if (tableView == self.tableView5) {
        item =  self.dataArray5[indexPath.row];
    }
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    
    ((EFMyOrderViewCell*)cell).operator = ^(NSInteger type,NSString* orderId){
        [self operatroTableView:tableView type:type orderId:orderId];
    };
    
    
//    if(([item isKindOfClass:[EFShoppingInvalidClearItem class]])){
//        ((EFShoppingInvalidClearCell *)cell).clearblock = ^(){
//            [self removeInvalidGoods];
//        };
//    }
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x/kScreenWidth;
    [self.segmentView setSeletIndex:index];
}


- (UITableView *)tableView1{
    if (!_tableView1) {
        _tableView1 = [self getATableView];
    }
    return _tableView1;
}

- (UITableView *)tableView2{
    if (!_tableView2) {
        _tableView2 = [self getATableView];
        _tableView2.left = kScreenWidth;
    }
    return _tableView2;
}

- (UITableView *)tableView3{
    if (!_tableView3) {
        _tableView3 = [self getATableView];
        _tableView3.left = kScreenWidth*2;
    }
    return _tableView3;
}


- (UITableView *)tableView4{
    if (!_tableView4) {
        _tableView4 = [self getATableView];
        _tableView4.left = kScreenWidth*3;
    }
    return _tableView4;
}


- (UITableView *)tableView5{
    if (!_tableView5) {
        _tableView5 = [self getATableView];
        _tableView5.left = kScreenWidth*4;
    }
    return _tableView5;
}



- (UITableView *)getATableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight -32.f)];
    tableView.delegate = self;
    tableView.dataSource = self;
    //tableView.contentInset = UIEdgeInsetsMake(15.f, 0, 0, 0);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];;
    return tableView;
}

-(EFCouponSegmentView*)segmentView{
    if(_segmentView == NULL){
        _segmentView = [[EFCouponSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 32)];
    }
    return _segmentView;
}


- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 32, kScreenWidth, kScreenHeight - kStatusBarHeight - 32)];
        _scrollView.contentSize = CGSizeMake(kScreenWidth * 5, _scrollView.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(void)operatroTableView:(UITableView *)tableView type:(NSInteger)type orderId:(NSString*)orderId{
    if (tableView == self.tableView1) {
        [self operatorData:self.dataArray1 orderId:orderId];
    }else if (tableView == self.tableView2) {
        [self operatorData:self.dataArray2 orderId:orderId];
    }else if (tableView == self.tableView3) {
        [self operatorData:self.dataArray3 orderId:orderId];
    }else if (tableView == self.tableView4) {
        [self operatorData:self.dataArray4 orderId:orderId];
    }else if (tableView == self.tableView5) {
        [self operatorData:self.dataArray5 orderId:orderId];
    }
    
    [tableView reloadData];
}

-(void)operatorData:(NSMutableArray*)array orderId:(NSString*)orderId{
    [APP_CONTEXT.net removeOrder:orderId OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            int index = -1;
            for(int i = 0 ; i < array.count; ++i){
                EFMyOrderViewItem* item = [self.dataArray1 objectAtIndex:i];
                if([item.id isEqual:orderId]){
                    index = i;
                    break;
                }
            }
            if(index != -1){
                [array removeObjectAtIndex:index];
                if(array.count == 0){
                    [self showEmptyImageView:@"bg_icon_order" message:@"您还未有任何订单"];
                }
            }
        }else{
            [[iToast makeText:message] show];
        }
    }];
  
}


@end
